#pragma once
#include "vn/BinaryReader.h"
#include "vn/MemoryStream.h"

void BinaryReaderTest() {_test_func_;
	{
		MemoryStream m("1234567890", 10);
		BinaryReader b(&m);

		assert( b.Read<char>() == '1');
		int i1 = b.Read<int>();
		int i2 = b.Read<int>();
		REQUIRE_EXCEPTION( b.Read<int>() );
		assert( b.Read<Char>() == '0' );
	}

	{
		MemoryStream m("12\r\n45\n\n890", 11);
		BinaryReader b(&m);
		String out;
		b.Read(out); assert(out == "12");
		b.Read(out); assert(out == "45");
		b.Read(out); assert(out == "");
		b.Read(out); assert(out == "890");
		REQUIRE_EXCEPTION( b.Read(out) );
	}

	{
		MemoryStream m("12\r45\n7890", 10);
		BinaryReader b(&m);
		String out;
		b.Read(out); assert(out == "12");
		b.Read(out); assert(out == "45");
		b.Read(out); assert(out == "7890");
		REQUIRE_EXCEPTION( b.Read(out) );
	}

	{
		MemoryStream m("", 0);
		BinaryReader b(&m);
		String out;
		REQUIRE_EXCEPTION( b.Read(out) );
	}

	{
		MemoryStream m("\n", 1);
		BinaryReader b(&m);
		String out;
		b.Read(out);
		REQUIRE_EXCEPTION( b.Read(out) );
	}


	{
		MemoryStream m("1234567890", 10);
		BinaryReader b(&m);
		REQUIRE_EXCEPTION( b.SeekOver("ABC", 3) ); // not found
		b.SeekOver("567", 3);
		String out;
		b.Read(out); assert(out == "890");
	}

	{
		MemoryStream m("1234567890", 10);
		BinaryReader b(&m);
		b.m_bufstm.PrepareBuffer(5);
		Buf8 buf[100];
		assert( b.Read(buf, 2) == 2);
		assert( b.Read(buf+2, 100) == 8);
		assert(Mem::Compare(buf, "1234567890", 10) == 0);
	}
}
